﻿using Microsoft.AspNetCore.Mvc;
using mozhi.smarterp.AuthorCenter.Identities.Clients;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;

namespace mozhi.smarterp.AuthorCenter.Controllers
{
    [RemoteService]
    public class OpenIddictClientTypeController : AuthorCenterController
    {
        private IOpenIddictClientTypeAppService _appService;
        public OpenIddictClientTypeController(IOpenIddictClientTypeAppService appService)
        {
            _appService = appService;
        }

        [HttpGet]
        [Route("api/identity/openIddict/clients/type/{id}")]
        public async Task<OpenIddictClientTypeDto> GetAsync(Guid id)
        {
            return await _appService.GetAsync(id);
        }

        [HttpGet]
        [Route("api/identity/openIddict/clients/type")]
        public async Task<PagedResultDto<OpenIddictClientTypeDto>> GetListAsync(OpenIddictClientTypeFilter input)
        {
            return await _appService.GetListAsync(input);
        }

        [HttpGet]
        [Route("api/identity/openIddict/clients/{clientId}/type")]
        public async Task<OpenIddictClientTypeDto> GetAsync(string clientId)
        {
            if (clientId.IsNullOrEmpty())
                return default;

            var clientTypes = await _appService.GetListAsync(new OpenIddictClientTypeFilter() { ClientId = clientId, MaxResultCount = 1, SkipCount = 0 });

            return clientTypes?.Items?.FirstOrDefault();
        }

        [HttpPost]
        [Route("api/identity/openIddict/clients/type")]
        public async Task<OpenIddictClientTypeDto> CreateAsync(OpenIddictClientTypeCreateDto input)
        {
            return await _appService.CreateAsync(input);
        }

        [HttpPut]
        [Route("api/identity/openIddict/clients/type/{id}")]
        public async Task<OpenIddictClientTypeDto> UpdateAsync(Guid id, OpenIddictClientTypeUpdateDto input)
        {
            return await _appService.UpdateAsync(input);
        }

        [HttpDelete]
        [Route("api/identity/openIddict/clients/type/{id}")]
        public async Task DeleteAsync(Guid id)
        {
            await _appService.DeleteAsync(id);
        }
    }
}
